import 'dart:convert';

import 'package:get/get.dart';
import 'package:meePet/bean/TabConfigData.dart';
import 'package:meePet/utils/LogUtils.dart';
import 'package:meePet/utils/ext/StringExt.dart';

import '../base/LiveData.dart';
import '../manager/TabConfigManager.dart';

class MainPageVM extends GetxController {
  LiveData<List<Tabs>> tabLd = LiveData();
  Data tabConfigData = Data();
  String? argsStr;

  @override
  void onInit() {
    LogUtils.i("这里执行在onResume之后了");
    var bundle = Get.parameters;
    argsStr = bundle['data'].toString();
    LogUtils.i("获取的页面传递数据:${argsStr ?? ""}");

    if (!argsStr.isNulOrEmpty()) {
      _refreshByTabData(argsStr!);
    } else {
      LogUtils.i("直接拿本地数据");
      /*TabConfigManager.getInstance()
          .getConfig()
          .then((configJson) => {_refreshByTabData(configJson)});*/
      _refreshByTabData( TabConfigManager.getInstance().getConfigLocal());
    }

    super.onInit();
  }

  @override
  void onReady() {
    super.onReady();
  }

  void _refreshByTabData(String jsonStr) {
    LogUtils.i("原始数据是啥:${jsonStr}");
    tabConfigData = Data.fromJson(json.decode(jsonStr));
    LogUtils.i("解析传递数据:${tabConfigData.toJson()}");
    tabLd.post(tabConfigData.tabs);
    LogUtils.i("通知数据变化:${tabConfigData.tabs.toString()}");
  }

  void requestTabData() {
    tabLd.post(tabConfigData.tabs);
  }

  @override
  void onClose() {
    //tabLd.doDestroy();
    super.onClose();
  }
}
